define(function(require, exports, module){

	var Backbone = require('backbone'),
            util = require('{baseSea}util/util'),
            lang = require('{baseSea}I18N/{locale}');

	var JoinusModel = Backbone.Model.extend({

                initialize: function(){
		     this.bind("validate",function(model,error){});
	        },

		defaults: {
                    username: null,
		    email: null,
		    password: null,
                    img: null,
	            gender: null,
 	            age: null,
	            r_country: null,
	            r_zip: null
		},
 
                url : seajs.data.vars.apiUrl+'user_register',
          
		validate: function(attrs) {
		    var arr=[],tthis=this,bool = false;
 		    for (var x in attrs) {
		         var obj =  tthis.doFieldValidata(x,attrs);
			     arr.push(obj);
			     if (!obj.bool) {                   
			         bool = true;
			     } 
		    }
                   
                    if (bool) {
                        this.trigger("validate", this, arr);
		        return arr;
                    } else {
                        this.trigger("validate", this, arr);
                    } 
	        },

                doFieldValidata : function(field,model) {
		    var obj = {bool : true, field:field};
		    switch(field) {
		       case 'email' :
			     if (model[field] == null || $.trim(model[field]) == '') { 
				 obj.bool = false;
				 obj.msg = lang.required;
			     } else if (!util.isEmail(model[field])) {
				 obj.bool = false;
				 obj.msg = lang.email_format_error;
			     }
		       break;
	 
		       case 'username': 
			     if (model[field] == null || $.trim(model[field]) == '') {
				 obj.bool = false;
				 obj.msg = lang.required;
			     } else if (!util.isLength(model[field],6,18)) {
				 obj.bool = false;
				 obj.msg = lang.username_format_error+'[6-18].';
			     }
		       break;

		       case 'password':
			     if (model[field] == null || $.trim(model[field]) == '') {
				 obj.bool = false;
				 obj.msg = lang.required;
			     } else if (!util.isLength(model[field],6,18)) {
				 obj.bool = false;
				 obj.msg = lang.password_format_error+'[6-18].';
			     }
		       break;
                
                        case 'lookForGender':
		        case 'gender':
			    if (model[field] == null || model[field] == '') {
				obj.bool = false;
				obj.msg = lang.selected;
			    }
		        break;     
		        case 'age':
		        case 'r_country':
		        case 'r_zip':
			    if (model[field] == null || model[field] == '') {
				obj.bool = false;
				obj.msg = lang.required;
			    }                   
		        break;  
		        case 'img':
		        break;
		        case 'lookForAge' :
			    if (model[field] == null || model[field] == '') {
				obj.bool = false;
				obj.msg = lang.required;
			    } else {
				var arr = model[field].split('-');
				if (arr[1]<arr[0]) {
				    obj.bool = false;
				    obj.msg = lang.joinus_lookfor_age;
				}
			       
			    }
		        break;
 

		    }     
		    return obj;
	    }                  

	});

	module.exports = JoinusModel;
});
